import time
from gevent import monkey

monkey.patch_all()  # 必须写在最上面，这句话后面的所有阻塞全部能够识别了

import gevent  # 直接导入即可
import time
import requests
import app

class CreateOrder:

    def __init__(self):
        # 创建订单
        self.post_create_order_url = "https"+app.BASE_URL + "/vega-server/cxfx/groupbuy/createOrder"
        # 取消订单
        self.post_close_order_url = "https"+app.BASE_URL + "/vega-server/cxfx/groupbuy/closeOrder"
        # 订单列表
        self.post_order_list_url = "https"+app.BASE_URL +"/vega-server/cxfx/groupbuy/findOrderPageQuery"
    # 创建订单
    def post_create_order(self,name,phone,num,groupNo,goodsNo):
        myJson = {"groupNo":groupNo,"name":name,"phone":phone,"detailList":[{"goodsNo":goodsNo,"num":num}]}
        myHeads = {"Content-Type": "application/json","token":app.TOKEN}
        return requests.post(url=self.post_create_order_url,json=myJson,headers=myHeads,verify=False)

    def post_create_order_zyf(self,name,phone):
        myJson = {"groupNo":"T1033001","name":name,"phone":phone,"detailList":[{"goodsNo":"001060","num":1}]}
        myHeads = {"Content-Type": "application/json","token":app.Token_zyf}
        return requests.post(url=self.post_create_order_url,json=myJson,headers=myHeads,verify=False)

    # 取消订单
    def post_close_order(self,orderNo):
        myJson = "orderNo={}".format(orderNo)
        myHeads = {"Content-Type": "application/x-www-form-urlencoded", "token": app.Token_zyf}
        return requests.post(url=self.post_close_order_url, data=myJson, headers=myHeads, verify=False)

    # 订单列表
    def post_order_list(self,state):
        """
        :param state: 1:待支付 2:待收货 3:已完成 4:已取消 5:已退款
        :return:
        """
        myJson = "pageNumber=1&pageSize=10&queryOrderType=1&orderState={}".format(state)
        myHeads = {"Content-Type": "application/x-www-form-urlencoded", "token": app.Token_zyf}
        return requests.post(url=self.post_order_list_url, data=myJson, headers=myHeads, verify=False)

if __name__ == '__main__':
    # 下单
    # print(CreateOrder().post_create_order("张鹏", "15347099941",2).json())
    # # print(CreateOrder().post_create_order_zyf("张一飞", "13871226252").json())
    # # a = CreateOrder().post_create_order("张鹏", "15347099941").json()["data"]
    # # print(CreateOrder().post_close_order(a).json())
    # 取消订单
    # w = CreateOrder().post_close_order("2020042017172800046002").json()
    # print(w)
    # 订单列表
    a = CreateOrder().post_order_list(1).json()
    print(a)
